Event.observe(window, 'load', function() {
    $$('table.datatable tr:nth-child(odd)').each(function (obj){
        if(obj.className == '') {
            obj.addClassName('alt');
        }
    });

    $$('img.datepicker').each(function (img){
        var input = img.up(1).down('input');
        Calendar.setup({
            inputField : input.id,
            trigger    : img.id,
            dateFormat : "%d/%m/%Y",
            onSelect   : function() {
                this.hide()
            }
        });
    });

    initForm();
});

var highlight_array = new Array();

function initForm(){
    browserDetect();
    initializeFocus();
    protectOnDoubleSubmit();
    showRangeCounters();
}

function protectOnDoubleSubmit() {
    $$('mainform').each(function (element) {
        disableSubmitButton();
    });
}

function disableSubmitButton() {
//need to locate the submit button
//document.getElementById('saveForm').disabled = true;
}

// for radio and checkboxes, they have to be cleared manually, so they are added to the
// global array highlight_array so we dont have to loop through the dom every time.
function initializeFocus(){
    fields = getElementsByClassName(document, "*", "field");
    for(i = 0; i < fields.length; i++) {
        if(fields[i].type == 'radio' || fields[i].type == 'checkbox' || fields[i].type == 'file') {
            fields[i].onclick = function(){
                clearSafariRadios();
                addClassName(this.parentNode.parentNode.parentNode, "focused", true)
            };
            fields[i].onfocus = function(){
                clearSafariRadios();
                addClassName(this.parentNode.parentNode.parentNode, "focused", true)
            };
            highlight_array.splice(highlight_array.length,0,fields[i]);
        }
        else if(fields[i].className.match('addr')){
            fields[i].onfocus = function(){
                clearSafariRadios();
                addClassName(this.parentNode.parentNode.parentNode, "focused", true)
            };
            fields[i].onblur = function(){
                removeClassName(this.parentNode.parentNode.parentNode, "focused")
            };
        }
        else if(fields[i].className.match('other')){
            fields[i].onfocus = function(){
                clearSafariRadios();
                addClassName(this.parentNode.parentNode.parentNode, "focused", true)
            };
        }
        else {
            fields[i].onfocus = function(){
                clearSafariRadios();
                addClassName(this.parentNode.parentNode, "focused", true)
            };
            fields[i].onblur = function(){
                removeClassName(this.parentNode.parentNode, "focused")
            };
        }
    }
}

function clearSafariRadios() {
    for(var i = 0; i < highlight_array.length; i++) {
        if(highlight_array[i].parentNode) {
            removeClassName(highlight_array[i].parentNode.parentNode.parentNode, 'focused');
        }
    }
}

function browserDetect(){
    var detect = navigator.userAgent.toLowerCase();
    var container = document.getElementsByTagName('html');
    if(detect.indexOf('safari') + 1){
        addClassName(container[0], 'safari', true);
    }
    if(detect.indexOf('firefox') + 1){
        addClassName(container[0], 'firefox', true);
    }
}

function showRangeCounters(){
    counters = getElementsByClassName(document, "em", "currently");
    for(i = 0; i < counters.length; i++) {
        counters[i].style.display = 'inline';
    }
}

function validateRange(ColumnId, RangeType) {
    if(document.getElementById('rangeUsedMsg'+ColumnId)) {
        var field = document.getElementById('Field'+ColumnId);
        var msg = document.getElementById('rangeUsedMsg'+ColumnId);

        switch(RangeType) {
            case 'character':
                msg.innerHTML = field.value.length;
                break;

            case 'word':
                var val = field.value;
                val = val.replace(/\n/g, " ");
                var words = val.split(" ");
                var used = 0;
                for(i =0; i < words.length; i++) {
                    if(words[i].replace(/\s+$/,"") != "") used++;
                }
                msg.innerHTML = used;
                break;

            case 'digit':
                msg.innerHTML = field.value.length;
                break;
        }
    }
}

/*--------------------------------------------------------------------------*/

//http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

//http://www.bigbold.com/snippets/posts/show/2630
function addClassName(objElement, strClass, blnMayAlreadyExist){
    if ( objElement.className ){
        var arrList = objElement.className.split(' ');
        if ( blnMayAlreadyExist ){
            var strClassUpper = strClass.toUpperCase();
            for ( var i = 0; i < arrList.length; i++ ){
                if ( arrList[i].toUpperCase() == strClassUpper ){
                    arrList.splice(i, 1);
                    i--;
                }
            }
        }
        arrList[arrList.length] = strClass;
        objElement.className = arrList.join(' ');
    }
    else{
        objElement.className = strClass;
    }
}

//http://www.bigbold.com/snippets/posts/show/2630
function removeClassName(objElement, strClass){
    if ( objElement.className ){
        var arrList = objElement.className.split(' ');
        var strClassUpper = strClass.toUpperCase();
        for ( var i = 0; i < arrList.length; i++ ){
            if ( arrList[i].toUpperCase() == strClassUpper ){
                arrList.splice(i, 1);
                i--;
            }
        }
        objElement.className = arrList.join(' ');
    }
}